package sample.viewassets.contact
{
	import asf.forms.Form;
	import asf.forms.FormField;
	import asf.forms.FormFieldError;
	import asf.utils.FX;
	
	import br.pedromoraes.btween.ISequenceable;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import sample.components.SendButton;
	import sample.viewassets.AContactForm;

	public class ContactForm extends AContactForm
	{
		private var frm:Form;
		private var button:SendButton;
		
		public function ContactForm( )
		{
			super( );
		}
		
		public function init( frmInstance:Form ):void
		{
			var field:FormField;
			var fields:Array = frmInstance.getFields( );
			
			frm = frmInstance;
			button = new SendButton( );
			
			addChild( button );
			
			button.y = frm.y + 230;
			button.x = frm.x;
			
			button.addEventListener( MouseEvent.CLICK, sendClick );
			
			for each( field in fields )
			{
				field.alpha = 0;
			}
			
			button.alpha = 0;
		}
		
		public function show( delay:uint ):ISequenceable
		{
			var field:FormField;
			var fields:Array = frm.getFields( );
			var res:ISequenceable;
			var n:uint = 0;
			
			for each( field in fields )
			{
				res = FX.fadeIn( ( field.view as DisplayObject ), 300, ( n * 100 ) + delay );
				n++;
			}
			
			res = FX.fadeIn( button, 300, ( n * 100 ) + delay );
			
			return res;
		}
		
		private function sendClick( evt:MouseEvent ):void
		{
			var errors:Array = frm.getErrors( );
			var result:Object = frm.getResult( );
			var error:FormFieldError;
			
			for each( error in errors )
			{
				ctrace( "ERROR: " + error.message );
			}
			
			if( !frm.isValid( ) )
			{
				frm.markAllErrors( );
			}
			else
			{
				dtrace( frm.getResult( ) );
			}
		}
		
		public function dispose( ):void
		{
			removeChild( button );
			
			button.removeEventListener( MouseEvent.CLICK, sendClick );
			
			button = null;
			frm = null;
		}
	}
}